﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineCinemaBooking.BookingService;
using CinemaLib;

namespace OnlineCinemaBooking
{
    public partial class _Default : System.Web.UI.Page
    {
        List<Movie> movies;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                using (BookingService.BookingServiceClient proxy = new BookingServiceClient())
                {
                    proxy.Open();
                    movies = proxy.GetNewestMovies(10);
                    movies.OrderBy(m => m.ID);
                    foreach (Movie m in movies)
                    {
                        ImageButton ib = new ImageButton();
                        ib.CssClass = "imgRight";
                        ib.Click += new ImageClickEventHandler(ImageButton_Click);
                        ib.ID = m.Name;

                        MoviePanel.Controls.Add(ib);
                        MoviePanel.Controls.Add(new LiteralControl("<p class=\"content\"> " +
                            "<b>" + m.Name + "</b> " +
                            "<br/> " +
                            m.Description +
                            "</p>"));

                    }
                }
            }
            catch (HttpException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.FaultException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (TimeoutException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (NullReferenceException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
            catch (Exception ex)
            {
                Session["Error"] = ex;
                Response.Redirect("ErrorPage.aspx");
            }
        }

        void ImageButton_Click(object sender, ImageClickEventArgs e)
        {
            Session["MovieData"] = movies.Find(m => m.Name == (sender as ImageButton).ID);
            Session["TimeStamp"] = DateTime.Now;
            Response.Redirect("BookNow.aspx");
        }
    }
}
